﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Basic4Net.OAWeb.Base;
using System.Data;
using Basic4Net.Utility.Common;
using System.Web.Services;

namespace Basic4Net.OAWeb.Module_Accounting
{
    public partial class Adjust : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                classbind();
                typebind();
            }
        }

        private void classbind()
        {
            this.ddl_FType.DataValueField = "RID";
            this.ddl_FType.DataTextField = "PostName";
            this.ddl_FType.DataSource = (new BusinessRules.Post()).FindAll(SchoolMark);
            this.ddl_FType.DataBind();
            ListItem item1 = new ListItem();
            item1.Value = "0";
            item1.Text = "尚未分类";
            this.ddl_FType.Items.Add(item1);
            ListItem item2 = new ListItem();
            item2.Value = "-1";
            item2.Text = "----全部----";
            this.ddl_FType.Items.Add(item2);
            this.ddl_FType.SelectedValue = "-1";
        }

        private void typebind()
        {
            DataTable dt = new DataTable();
            Common.IListToTable(dt, new BusinessRules.ListAdjust());
            IList<BusinessRules.ListAdjust> mylist = (new BusinessRules.Adjust()).ListTeacher(GetProDateYear(), GetProDateMonth(), int.Parse(this.ddl_FType.SelectedValue), this.txt_TeacherCode.Text, this.txt_TeacherName.Text);
            for (int i = 0; i < mylist.Count; i++)
            {
                DataRow row = dt.NewRow();
                row["TeacherID"] = mylist[i].TeacherID;
                row["TeachCode"] = mylist[i].TeachCode;
                row["TeachName"] = mylist[i].TeachName;
                row["TypeName"] = mylist[i].TypeName;
                row["SubsidyCls"] = mylist[i].SubsidyCls;
                row["Holwithhold"] = mylist[i].Holwithhold;
                row["WithholdMoney"] = mylist[i].WithholdMoney;
                row["HolidayWork"] = mylist[i].HolidayWork;
                row["AmountDiffer"] = mylist[i].AmountDiffer;
                row["MeritPayID"] = mylist[i].MeritPayID;
                row["HolidayWorkContent"] = mylist[i].HolidayWorkContent;
                row["AmountDifferContent"] = mylist[i].AmountDifferContent;
                dt.Rows.Add(row);
            }

            //------------------------------------------------------------------------分页处理开始......
            int PageNow = 0;
            DataView dv = dt.DefaultView;
            System.Web.UI.WebControls.PagedDataSource ps = new PagedDataSource();
            ps.DataSource = dv;
            ps.AllowPaging = true;
            ps.PageSize = 10;
            if (this.txt_Go.Text.ToString() == "")
                this.txt_Go.Text = "1";
            if (this.txt_Go.Text.ToString() == "0")
                this.txt_Go.Text = "1";
            if (Convert.ToInt32(this.txt_Go.Text.ToString()) >= ps.PageCount)
                this.txt_Go.Text = ps.PageCount.ToString();
            PageNow = Convert.ToInt32(this.txt_Go.Text.ToString()) - 1;
            //this.lbn_Up.Enabled = true;
            //this.lbn_Next.Enabled = true;
            //if (PageNow == 0)
            //{
            //    this.lbn_Up.Enabled = false;
            //}
            //if (PageNow == ps.PageCount - 1)
            //{
            //    this.lbn_Next.Enabled = false;
            //}
            ps.CurrentPageIndex = Convert.ToInt32(this.txt_Go.Text.ToString()) - 1;
            //-------------------------------------------------------------------------分页处理结束

            this.rpt_TeacherType.DataSource = ps;
            this.rpt_TeacherType.DataBind();

            //------------------------------------------------------------------------记录总数、当前页数、总页数处理开始......
            //this.lbl_SumCount.Text = ds.Tables[0].Rows.Count.ToString();
            this.lbl_ToPage.Text = this.txt_Go.Text.ToString();
            this.lbl_PageCount.Text = ps.PageCount.ToString();

            //------------------------------------------------------------------------------记录总数、当前页数、总页数处理结束
        }

        protected void btnBind_Click(object sender, EventArgs e)
        {
            typebind();
        }

        [System.Web.Services.WebMethod]
        public static bool ChangeData(int MeritpayID,int type,object value)
        {
            bool result = true;
            string msg = string.Empty;
            BusinessRules.Adjust bll = new BusinessRules.Adjust();
            switch (type)
            {
                case 1:
                    result = bll.ChangeSubsidyCls(MeritpayID, (float)Convert.ToDouble(value),ref msg);
                    if (result)
                        bll.ChangeAmount(GetProDateYear(), GetProDateMonth(), ref msg);
                    break;
                case 2:
                    result = bll.ChangeHolidayWork(MeritpayID, (float)Convert.ToDouble(value), ref msg);
                    if (result)
                        bll.ChangeAmount(GetProDateYear(), GetProDateMonth(), ref msg);
                    break;
                case 3:
                    result = bll.ChangeAmountDiffer(MeritpayID, (float)Convert.ToDouble(value), ref msg);
                    if (result)
                        bll.ChangeAmount(GetProDateYear(), GetProDateMonth(), ref msg);
                    break;
                case 4:
                    result = bll.ChangeHolidayWorkContent(MeritpayID, Convert.ToString(value), ref msg);
                    if (result)
                        bll.ChangeAmount(GetProDateYear(), GetProDateMonth(), ref msg);
                    break;
                case 5:
                    result = bll.ChangeAmountDifferContent(MeritpayID, Convert.ToString(value), ref msg);
                    if (result)
                        bll.ChangeAmount(GetProDateYear(), GetProDateMonth(), ref msg);
                    break;
            }
            return result;
        }

        [System.Web.Services.WebMethod]
        public static void ChangeHolwithhold(bool check)
        {
            bool result = true;
            string msg = string.Empty;
            BusinessRules.Adjust bll = new BusinessRules.Adjust();
            if (check)
            {
                result = bll.ChangeHolwithhold(GetProDateYear(), GetProDateMonth(), 600, ref msg);
                if (result)
                    bll.ChangeAmount(GetProDateYear(), GetProDateMonth(), ref msg);
            }
            else
            {
                result = bll.ChangeHolwithhold(GetProDateYear(), GetProDateMonth(), 0, ref msg);
                if (result)
                    bll.ChangeAmount(GetProDateYear(), GetProDateMonth(), ref msg);
            }
        }

        [System.Web.Services.WebMethod]
        public static void ChangeWithholdMoney(bool check)
        {
            bool result = true;
            string msg = string.Empty;
            BusinessRules.Adjust bll = new BusinessRules.Adjust();
            result = bll.ChangeWithholdMoney(GetProDateYear(), GetProDateMonth(), check, ref msg);
            if (result)
                bll.ChangeAmount(GetProDateYear(), GetProDateMonth(), ref msg);
        }

        protected string GetHolwithholdState()
        {
            if ((new BusinessRules.Adjust()).GetHolwithholdState(GetProDateYear(),GetProDateMonth()))
                return "checked='checked'";
            else
                return "";
        }

        protected string GetWithholdMoneyState()
        {
            if ((new BusinessRules.Adjust()).GetWithholdMoneyState(GetProDateYear(), GetProDateMonth()))
                return "checked='checked'";
            else
                return "";
        }
    }
}